﻿using ModbusDataParseTool.Entity;
using ModbusDataParseTool.Service;
using MyUtility.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ModbusDataParseTool.Model
{
    public class ProtocolModel
    {

        public List<BaseInfoCfgEntity> BaseInfoCfgEnts = new List<BaseInfoCfgEntity>();
        public Dictionary<long, ModbusProtocolEntity> ProtocalKeyValuePairs = new Dictionary<long, ModbusProtocolEntity>();
             

        public ProtocolModel()
        {
            InitData();
        }


        private void InitData()
        {
            GetConfigService getConfigService = SingletonProvider<GetConfigService>.Instance;
            List<BaseInfoCfgEntity> baseInfoCfgEntTemps = new List<BaseInfoCfgEntity>();
            if (getConfigService.ReadBaseInfo(ref baseInfoCfgEntTemps))
            {
                foreach (BaseInfoCfgEntity item in baseInfoCfgEntTemps)
                {
                    Protocol protocol = null;
                    string path = "CFG\\" + item.ProtocolFile;
                    if (getConfigService.ReadStandardXMLToClass<Protocol>(path, ref protocol))
                    {
                        if (protocol != null)
                        {
                            long guid = MyUtility.Utility.CreateGuid.GuidToLongID();
                            item.ProtocolCode = guid;
                            ModbusProtocolEntity modbusProtocolEntity = new ModbusProtocolEntity()
                            {
                                Order = item.BaseInfoOrder,
                                ProtocolName = item.ProtocolName,
                                ProtocolUnits = protocol.Units.ToList()
                            };
                            ProtocalKeyValuePairs.Add(guid, modbusProtocolEntity);
                            BaseInfoCfgEnts.Add(item);
                        }
                    }
                }
            }
            

        }

    }
}
